﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SK.Cores._Extension
{  
    public class Path : SK.Cores._Base.Context
    {
        #region Private
        private string _RelativeRoot;
        private string _RelativePhysic;

        private void InitPath(string relativeRoot, string relativePhysic)
        {
            RelativeRoot = relativeRoot;
            RelativePhysic = relativePhysic;
        }
        #endregion Private

        #region Public(Non-Static)
        public Path() 
        {
            InitPath("", "");
        }
        public Path(string relativePhysic) // Ex.: new Path("", "Contents/Images/Product/ABC.png");
        {
            this.InitPath("", relativePhysic);
        }
        public Path(string relativeRoot, string relativePhysic) // Ex.: new Path("Contents/Images/", "Product/ABC.png");
        {
            this.InitPath(relativeRoot, relativePhysic);
        }

        public string RelativeRoot
        {
            set
            {
                this._RelativeRoot = Path.FormatDirectoryPath(value);
            }
            get
            {
                return this._RelativeRoot;
            }
        }

        public string AbsolutePhysic
        {
            set
            {
                string absolutePhysic = Path.FormatDirectoryPath(value);

                if (!absolutePhysic.StartsWith(Path.PhysicalRoot + RelativeRoot)) this.RelativeRoot = "";

                this.RelativePhysic = Path.GetRelativePhysic(this.RelativeRoot, value);
            }
            get
            {
                return Path.GetAbsolutePhysic(this.RelativeRoot, this.RelativePhysic);
            }
        }
        public string RelativePhysic
        {
            set
            {
                this._RelativePhysic = Path.FormatDirectoryPath(value);
            }
            get
            {
                return this._RelativePhysic;
            }
        }
        public string VirtualPhysic
        {
            get
            {
                return GetVirtualPhysic(this.RelativeRoot, this.RelativePhysic);
            }
        }

        public string AbsoluteUrl
        {
            get
            {
                return Path.GetAbsoluteUrl(this.RelativeRoot, this.RelativeUrl);
            }
            set
            {
                this.RelativePhysic = Path.GetRelativeUrl(this.RelativeRoot, this.RelativePhysic);
            }
        }
        public string RelativeUrl
        {
            get
            {
                return _RelativePhysic;
            }
            set
            {
                _RelativePhysic = value;
            }
        }
        public string VirtualUrl
        {
            set 
            {
                this.RelativePhysic = Path.GetVirtualUrl(this.RelativeRoot, value);
            }
            get 
            {
                return Path.GetVirtualPhysic(this.RelativeRoot, this.RelativePhysic);
            }
        }
        #endregion Public(Non-Static)

        #region Public(Static)
        public enum SKPathType { Directory = 1, File = 2 } //Depend = 0, 

        public static string PhysicalRoot // Actually the application physical path. Ex.: C:/.../SKCores/
        {
            get
            {
                return Path.Format(Request.PhysicalApplicationPath, SKPathType.Directory);
            }
        }
        public static string UrlRoot // Ex.: http://localhost:8081/SKCores/
        {
            get
            {
                string retVal;

                string originalUrl = Request.Url.OriginalString;
                string absolutePath = Request.Url.AbsolutePath;

                retVal = originalUrl.Remove(originalUrl.IndexOf(absolutePath));

                return Path.FormatDirectoryPath(retVal);
            }
        }

        public static string Format(string originalPath, SKPathType type)
        {
            if (type == SKPathType.Directory)
                return Path.FormatDirectoryPath(originalPath);
            else
                return originalPath;
        }
        public static string FormatDirectoryPath(string originalPath) // format: use "/" not "\\"; start with no "/"; end with "/";
        {
            string retVal = originalPath.Replace("\\", "/");

            if (retVal.Length > 0 && retVal.StartsWith("/")) retVal = retVal.Remove(0, 1);
            if (!retVal.EndsWith("/")) retVal += "/";

            return retVal;
        }

        public static string GetAbsolutePhysic(string relativeRoot, string relativePhysic) // Entire physical path. Ex.: C:/.../SKCores/Contents/Iamges/Product/ABC.png
        {
            return PhysicalRoot + Path.FormatDirectoryPath(relativeRoot) + Path.FormatDirectoryPath(relativePhysic);
        }
        public static string GetRelativePhysic(string relativeRoot, string absolutePhysic) // Physical path from a specific folder. Ex.: (Contents/Images/)Product/ABC.png
        {
            return absolutePhysic.Replace(PhysicalRoot + Path.FormatDirectoryPath(relativeRoot), "");
        }
        public static string GetVirtualPhysic(string relativeRoot, string relativePhysic) // Physical path from the application root. Ex.: (C:/.../SKCores/)Contents/Images/Product/ABC.png
        {
            return Path.FormatDirectoryPath(relativeRoot) + Path.FormatDirectoryPath(relativePhysic);
        }

        public static string GetAbsoluteUrl(string relativeRoot, string relativeUrl) // Entire url. http://localhost:1008/SKCores/Contents/Images/Product/ABC.png
        {
            return Path.UrlRoot + Path.FormatDirectoryPath(relativeRoot) + Path.FormatDirectoryPath(relativeUrl);
        }
        public static string GetRelativeUrl(string relativeRoot, string absoluteUrl) // Url from an specific folder. Ex.: (Contents/Images/)Product/ABC.png
        {
            return absoluteUrl.Replace(Path.UrlRoot + Path.FormatDirectoryPath(relativeRoot), "").Replace("~/", "");
        }
        public static string GetVirtualUrl(string relativeRoot, string relativeUrl) // Url from the wwwroot or virtual directory. Ex.: (http://localhost:1008/SKCores/)Contents/Images/Product/ABC.png
        {
            return Path.FormatDirectoryPath(relativeRoot) + Path.FormatDirectoryPath(relativeUrl);
        }
        #endregion Public(Static)
    }
}
